const express = require('express');
const http = require('axios');
const router = express.Router();

// 企业微信回调 URL 验证
router.use('/callback', (req, res, next) => {
  const { msg_signature, timestamp, nonce, echostr  } = req.query || {};
  const token = 'glWxIiY8TqsG7sl6Ov2ot'; // 企业微信服务商后台，给应用配置的 Token

  // 重新计算签名

  // 与参数 msg_signature 检查是否一致
  if (msg_signature) {

  }
});

router.post('/login', async (req, res, next) => {
  console.log(req.body)
  https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
  try {
    // 获取第三方应用凭证
    const res = await http({
      method: 'POST',
      url: 'https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token',
      data: {
        suite_id: 'wwf1ab75171230ca78', // 用的唯一身份标识
        suite_secret: 'RDh5DpJFxlTVP80WpP8X2_vdML7FNuBEXpy-1vzOhFE', // 对应的调用身份密钥
        suite_ticket: ''
        // corpid: 'ww1fc9118d745e355d', // 每个服务商同时也是一个企业微信的企业，都有唯一的corpid
        // corpsecret: '0yoEO5P4yO4UitV_Gkn6pqGeSRiPTWBoDJcYLPQme3qjoDFcPASoFmEEus609Tpn' // 作为服务商身份的调用凭证
      }
    })
    console.log(res, "[---");
  } catch (error) {
    
  }

  try {
    
    // 获取第三方应用凭证
    const res = await http({
      method: 'GET',
      url: 'https://qyapi.weixin.qq.com/cgi-bin/service/miniprogram/jscode2session',
      data: {
        suite_access_token: '',
        js_code: '',
        grant_type: 'authorization_code'
      }
    })
    console.log(res, "[---");
  } catch (error) {
    
  }
  
  res.json({
    code: 200
  })
});

module.exports = router;